package com.itheima.sercive.CheckItemServiceImpl;

import com.itheima.common.entity.Result;
import com.itheima.mapper.OrderSettingMapper;
import com.itheima.pojo.OrderSetting;
import com.itheima.sercive.OrderSettingService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.List;

@Service
public class OrderSettingServiceImpl implements OrderSettingService {

    @Autowired
    private OrderSettingMapper mapper;


    /**
 * 批量保存订单设置信息
 * 该方法根据订单日期检查数据库中是否存在相应日期的设置如果存在，则更新设置；如果不存在，则插入新设置
 *
 * @param list 包含多个OrderSetting对象的列表，用于批量保存到数据库
 */
public void saveBatch(List<OrderSetting> list) {
    // 遍历订单设置列表
    for (OrderSetting os : list) {
        // 将订单日期转换为字符串格式，以便进行数据库查询
        String orderDateStr = new SimpleDateFormat("yyyy-MM-dd").format(os.getOrderDate());
        // 检查数据库中是否存在该日期的设置
        if (mapper.countByDate(orderDateStr) > 0) {
            // 如果存在，则更新设置
            mapper.update(os);
        } else {
            // 如果不存在，则插入新设置
            mapper.insert(os);
        }
    }
}



    public Result getOrderSettingByMonth(String month) {
        String start = month + "-01";
        String end = month + "-31";
        List<OrderSetting> data = mapper.findByMonth(start, end);
        return Result.success(data);
    }


    public Result editNumberByDate(OrderSetting orderSetting) {
        String orderDateStr = new SimpleDateFormat("yyyy-MM-dd").format(orderSetting.getOrderDate());
        if (mapper.countByDate(orderDateStr) > 0) {
            mapper.update(orderSetting);
        } else {
            orderSetting.setReservations(0);
            mapper.insert(orderSetting);
        }
        return Result.success("预约设置成功");
    }
}